package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.SesUserDao;
import com.bestescortbedsystem.dao.SesUserIdentityDao;
import com.bestescortbedsystem.dao.SesUserOrderDao;
import com.bestescortbedsystem.dao.SesUserOrdinaryDao;
import com.bestescortbedsystem.entity.*;
import com.bestescortbedsystem.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: UserServiceImpl
 * Package: com.bestescortbedsystem.service.impl
 *
 * @author: guanzepeng
 * @Date: 2021/2/24 09:48 星期三
 * @Description: 普通用户业务层
 */
@Service
public class UserServiceImpl implements UserService {

	@Resource
	private SesUserOrderDao sesUserOrderDao;

	@Resource
	private SesUserOrdinaryDao sesUserOrdinaryDao;

	@Resource
	private SesUserDao sesUserDao;

	@Resource
	private SesUserIdentityDao sesUserIdentityDao;

	/**
	 * 根据用户的UserID去查询用户的所有订单详情,以及用户的钱包金额数据 带分页
	 * @param userId 存储在session中的userID
	 * @param sesUserOrder 条件查询
	 * @param startMoney 根据订单金额进行区间查询 起始金额
	 * @param endMoney 结束金额
	 * @param pageNum 分页第几行 从0开始
	 * @param pageSize 分页第几页
	 * @return 返回Map集合 集合中order代表用户订单信息查询出的数据,list代表钱包数据,user代表用户数据,orderNumbers代表订单总数
	 */
	@Override
	public Map<String, Object> findUser(Long userId, SesUserOrder sesUserOrder, Float startMoney, Float endMoney, Integer pageNum, Integer pageSize) {
		SesUserOrderExample sesUserOrderExample = new SesUserOrderExample();
		SesUserOrderExample.Criteria criteria = sesUserOrderExample.createCriteria();
			//根据订单金额进行区间查询
			if(startMoney != 0.0 && endMoney != 0.0) {
				criteria.andUserorderrentBetween(startMoney, endMoney);
			}
			//订单编号模糊查询
			if(sesUserOrder.getUserordernum() != null) {
				criteria.andUserordernumLike("%" + sesUserOrder.getUserordernum() + "%");
			}
			//订单状态查询订单
			if(sesUserOrder.getUserorderstatus() != null) {
				criteria.andUserorderstatusEqualTo(sesUserOrder.getUserorderstatus());
			}
			//根据结算状态查询
			if(sesUserOrder.getUserordersettlementstatus() != null && !("0".equals(sesUserOrder.getUserordersettlementstatus()))) {
				criteria.andUserordersettlementstatusEqualTo(sesUserOrder.getUserordersettlementstatus());
			}
		//放入用户ID
		criteria.andUserUseridEqualTo(userId);
		//分页查询用户订单信息
		PageHelper.startPage(pageNum,pageSize);
		List<SesUserOrder> o = sesUserOrderDao.selectByExample(sesUserOrderExample);
		PageInfo<SesUserOrder> order = new PageInfo<>(o);
		//查询用户钱包信息
		SesUserOrdinaryExample sesUserOrdinaryExample = new SesUserOrdinaryExample();
		sesUserOrdinaryExample.createCriteria().andUseridEqualTo(userId);
		List<SesUserOrdinary> list = sesUserOrdinaryDao.selectByExample(sesUserOrdinaryExample);
		//查询用户姓名
		SesUser user = sesUserDao.selectByPrimaryKey(userId);
		//查询订单总数
		SesUserOrder s = new SesUserOrder();
		s.setUserUserid(userId);
		Integer number = sesUserOrderDao.selectFindByNumber(s);
		//查询用户真实姓名和银行卡号
		SesUserIdentityExample sesUserIdentityExample = new SesUserIdentityExample();
		sesUserIdentityExample.createCriteria().andUseridentityidEqualTo(user.getUserUserIdentityid());
		List<SesUserIdentity> sesUserIdentities = sesUserIdentityDao.selectByExample(sesUserIdentityExample);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("order",order);
		map.put("list",list);
		map.put("user",user);
		map.put("orderNumbers",number);
		map.put("zhenshi", sesUserIdentities.get(0));
		return map;
	}

	/**
	 * 将用户头像路径传入数据库中
	 * @param head 传入头像路径以及用户ID
	 * @return 返回Boolean
	 */
	@Override
	public Boolean insertPhoto(Head head) {
		SesUser sesUser = new SesUser();
		SesUserExample sesUserExample = new SesUserExample();
		SesUserExample.Criteria criteria = sesUserExample.createCriteria();
		criteria.andUseridEqualTo(Long.valueOf(head.getId()));

		sesUser.setUserphoto(head.getHeadAddress());
		boolean b = sesUserDao.updateByExampleSelective(sesUser, sesUserExample) == 1;
		return b;
	}

	/**
	 * 用户充值钱包金额
	 * @param money 传入充值金额
	 * @param id 传入用户ID
	 * @return 返回充值后的钱包金额 如果失败返回-1
	 */
	@Override
	@Transactional
		public Float updateAddMoney(Float money, Long userId) {
		SesUserOrdinary sesUserOrdinary = new SesUserOrdinary();
		SesUserOrdinaryExample sesUserOrdinaryExample = new SesUserOrdinaryExample();
		sesUserOrdinaryExample.createCriteria().andUseridEqualTo(userId);
		List<SesUserOrdinary> list2 = sesUserOrdinaryDao.selectByExample(sesUserOrdinaryExample);
		Float m = list2.get(0).getOrdinarymoney();
		sesUserOrdinary.setOrdinarymoney(m + money);
		boolean b = sesUserOrdinaryDao.updateByExampleSelective(sesUserOrdinary, sesUserOrdinaryExample) == 1;
		if(b) {
			List<SesUserOrdinary> list = sesUserOrdinaryDao.selectByExample(sesUserOrdinaryExample);
			return list.get(0).getOrdinarymoney();
		}
		return -1f;
	}

	/**
	 * 用户充值押金
	 * @param money 传入押金金额
	 * @param userId 传入用户ID
	 * @return 返回充值后的押金金额 如果失败返回-1
	 */
	@Override
	@Transactional
	public Float updateDeposit(Float money, Long userId) {
		SesUserOrdinary sesUserOrdinary = new SesUserOrdinary();
		SesUserOrdinaryExample sesUserOrdinaryExample = new SesUserOrdinaryExample();
		sesUserOrdinaryExample.createCriteria().andUseridEqualTo(userId);
		List<SesUserOrdinary> list2 = sesUserOrdinaryDao.selectByExample(sesUserOrdinaryExample);
	  Float m =	list2.get(0).getOrdinarydeposit();
		sesUserOrdinary.setOrdinarydeposit(m + money);
		boolean b = sesUserOrdinaryDao.updateByExampleSelective(sesUserOrdinary, sesUserOrdinaryExample) == 1;
		if(b) {
			List<SesUserOrdinary> list = sesUserOrdinaryDao.selectByExample(sesUserOrdinaryExample);
			return list.get(0).getOrdinarydeposit();
		}
		return -1f;
	}

	/**
	 * 退还用户押金
	 * @param userId 传入用户ID
	 * @return 返回Boolean
	 */
	@Override
	@Transactional
	public Boolean depositRefund( Long userId) {
		SesUserOrdinaryExample sesUserOrdinaryExample = new SesUserOrdinaryExample();
		sesUserOrdinaryExample.createCriteria().andUseridEqualTo(userId);
		SesUserOrdinary sesUserOrdinary = new SesUserOrdinary();
		sesUserOrdinary.setOrdinarydeposit(0f);
		return sesUserOrdinaryDao.updateByExampleSelective(sesUserOrdinary, sesUserOrdinaryExample) == 1;
	}

	/**
	 * 执行删除订单操作,只有已完成的订单才可以成功删除
	 * @param userorderId 传入订单编号
	 * @return 返回Boolean
	 */
	@Override
	public Boolean deleteOrder(Integer userorderId) {
		SesUserOrder order = sesUserOrderDao.selectByPrimaryKey(Long.valueOf(userorderId));
		int o = order.getUserorderstatus();
		if(o == 4) {
			return sesUserOrderDao.deleteByPrimaryKey(Long.valueOf(userorderId)) == 1;
		}
		return false;
	}

	/**
	 * 根据传入的对象修改真实姓名和银行卡号
	 * @param sesUserIdentity 传入对象
	 * @return 返回Boolean
	 */
	@Override
	public Boolean updateIdentity(SesUserIdentity sesUserIdentity) {
		if(sesUserIdentity == null) {
			return false;
		}
		SesUserIdentityExample sesUserIdentityExample = new SesUserIdentityExample();
		sesUserIdentityExample.createCriteria().andUseridentityidEqualTo(sesUserIdentity.getUseridentityid());
		boolean b = sesUserIdentityDao.updateByExampleSelective(sesUserIdentity, sesUserIdentityExample) == 1;
		return b;
	}

	/**
	 * 强制结算功能
	 * @param userorderId 传入订单ID
	 * @return 返回Boolean
	 */
	@Override
	public Boolean updateUserOrder(Long userorderId) {
		if (userorderId == null) {
			return false;
		}
		SesUserOrder sesUserOrder = new SesUserOrder();
		sesUserOrder.setUserorderstatus(4);
		sesUserOrder.setUserordersettlementstatus("2");
		sesUserOrder.setUserorderabnormal("用户进行了强制结算!");
		SesUserOrderExample sesUserOrderExample = new SesUserOrderExample();
		sesUserOrderExample.createCriteria().andUserorderidEqualTo(userorderId);
		boolean b = sesUserOrderDao.updateByExampleSelective(sesUserOrder, sesUserOrderExample) == 1;
		return b;
	}


}
